namespace Billing2
{
    using System;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public class ucForgotPassword : UserControl
    {
        protected UpdatePanel ActionPanel;
        protected Button btnSend;
        protected Panel PanelDetails;
        protected TextBox txtLoginID;
        protected TextBox txtEmail;
        protected ucHighlight ucHighlight1;
        protected UpdatePanel updatePanel3;
        protected UpdateProgress UpdateProgress1;

        protected void btnSend_Click(object sender, EventArgs e)
        {
            if (this.txtLoginID.Text.Trim() == "")
            {
                this.ucHighlight1.ErrorMessage = "Please enter correct Login ID and Email";
            }
            else
            {
                string loginId = this.txtLoginID.Text;
                string email = this.txtEmail.Text;
                DBCommands2.TBL_UserProfile user = Common.db.UserProfile.select2("where LoginId = @LoginId and Email = @email", loginId, email);
                if (user == null)
                {
                    this.ucHighlight1.ErrorMessage = "Please enter correct Login ID and Email";
                    return;
                }

                int? times = Common.db.SelectInt(@"
                    select count(*)
                    from PasswordHistory
                    where LoginId = @LoginId
                        and Action = @Action
                        and CreatedAt > dateadd(day, -1, getdate())
                    ", loginId
                     , LoginUser.UserAction_ResetPassword);
                if (times >= ClsApplicationConfiguration.ResetPasswordTimesPerDay)
                    throw new Prompt("Password can only be changed " + ClsApplicationConfiguration.ResetPasswordTimesPerDay.ToString() + " times per day");

                ClsUserProfile clsProfile = new ClsUserProfile
                {
                    LoginId = loginId
                };
                DataTable dt = clsProfile.SelectByLoginID();
                if (dt != null)
                {
                    if (dt.Rows.Count > 0)
                    {
                        clsProfile.LoginPassword = ClsPasswordHelper.Encrypt(ClsPasswordHelper.Generate(8, 2, 1));
                        clsProfile.UpdateByLoginIdForPassword();
                        ClsEmailHelper cls = new ClsEmailHelper();
                        ClsEmailTemplate clsTemplate = new ClsEmailTemplate();
                        cls.IsBodyHtml = true;
                        clsTemplate.ResetPassword(clsProfile.LoginId, ClsPasswordHelper.Decrypt(clsProfile.LoginPassword));
                        cls.MailToAdd(dt.Rows[0]["Email"].ToString());
                        cls.Subject = clsTemplate.Subject;
                        cls.Body0 = clsTemplate.Body0;
                        string Msg = cls.SendEmail();
                        if (Msg.ToLower().Contains("fail"))
                        {
                            this.ucHighlight1.ErrorMessage = Msg;
                        }
                        else
                        {
                            this.ucHighlight1.Message = Msg;

                            Common.db.Exec(@"
                                insert into PasswordHistory(LoginId, LoginPassword, Action)
                                values(@LoginId, @LoginPassword, @Action)
                                ", LoginUser.LoginID
                                 , clsProfile.LoginPassword
                                 , LoginUser.UserAction_ResetPassword);
                        }
                    }
                    else
                    {
                        this.ucHighlight1.ErrorMessage = "Please enter correct Login ID and Email";
                    }
                }
                else
                {
                    this.ucHighlight1.ErrorMessage = "Please enter correct Login ID and Email";
                }
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.ucHighlight1.CloseAll();
        }
    }
}
